package ntd.util.frame.ssh;

import cn.hutool.core.lang.Opt;
import cn.hutool.log.StaticLog;
import com.jcraft.jsch.*;

public class ToolJsch {

    private String username;
    private String password;
    private String host;
    private Integer port;

    public ToolJsch(String username, String password, String host, Integer port) {
        this.username = username;
        this.password = password;
        this.host = host;
        this.port = port;
    }


    public static void main(String[] args) {
        String localFilePath = "/path/to/local/video.mp4";
        String remoteFilePath = "/path/to/remote/video.mp4";

        ToolJsch toolJsch = new ToolJsch("fmhb", "fmhb2022", "116.255.130.173", 21);
    }

    private void upload(String localFilePath, String remoteFilePath) {
        JSch jsch = new JSch();
        Session session = null;
        try {
            session = jsch.getSession(username, host, port);
            session.setConfig("StrictHostKeyChecking", "no");
            session.setPassword(password);
            session.connect();

            ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
            channelSftp.connect();

            channelSftp.put(localFilePath, remoteFilePath);

            channelSftp.disconnect();
        } catch (JSchException | SftpException e) {
            StaticLog.error(e, "[Jsch][上传][{}]失败...");
        } finally {
            Opt.ofNullable(session).ifPresent(Session::disconnect);
        }
    }
}
